<?php

define('NETFLIX_API_OAUTH', 'http://api.netflix.com/oauth');
define('NETFLIX_API_OAUTH_REQUEST_TOKEN', NETFLIX_API_OAUTH.'/request_token');
define('NETFLIX_API_OAUTH_ACCESS_TOKEN', NETFLIX_API_OAUTH.'/access_token');

define('NETFLIX_API_CATALOG', 'http://api.netflix.com/catalog');
define('NETFLIX_API_CATALOG_TITLES', NETFLIX_API_CATALOG.'/titles');
define('NETFLIX_API_CATALOG_TITLES_INDEX', NETFLIX_API_CATALOG_TITLES.'/index');
define('NETFLIX_API_CATALOG_TITLES_FULL', NETFLIX_API_CATALOG_TITLES.'/full?v=2.0');

define('NETFLIX_API_USERS', 'http://api.netflix.com/users');
define('NETFLIX_API_USERS_QUEUES', NETFLIX_API_USERS.'/%s/queues');
define('NETFLIX_API_USERS_QUEUES_DISC', NETFLIX_API_USERS_QUEUES.'/disc');
define('NETFLIX_API_USERS_QUEUES_DISC_AVAILABLE', NETFLIX_API_USERS_QUEUES_DISC.'/available');
define('NETFLIX_API_USERS_QUEUES_DISC_AVAILABLE_ENTRY', NETFLIX_API_USERS_QUEUES_DISC_AVAILABLE.'/%s');
define('NETFLIX_API_USERS_QUEUES_DISC_SAVED', NETFLIX_API_USERS_QUEUES_DISC.'/saved');
define('NETFLIX_API_USERS_QUEUES_DISC_SAVED_ENTRY', NETFLIX_API_USERS_QUEUES_DISC_SAVED.'/%s');
define('NETFLIX_API_USERS_QUEUES_INSTANT', NETFLIX_API_USERS_QUEUES.'/instant');
define('NETFLIX_API_USERS_QUEUES_INSTANT_AVAILABLE', NETFLIX_API_USERS_QUEUES_INSTANT.'/available');
define('NETFLIX_API_USERS_QUEUES_INSTANT_AVAILABLE_ENTRY', NETFLIX_API_USERS_QUEUES_INSTANT_AVAILABLE.'/%s');
define('NETFLIX_API_USERS_QUEUES_INSTANT_SAVED', NETFLIX_API_USERS_QUEUES_INSTANT.'/saved');
define('NETFLIX_API_USERS_QUEUES_INSTANT_SAVED_ENTRY', NETFLIX_API_USERS_QUEUES_INSTANT_SAVED.'/%s');

/**
 * Netflix
 * 
 * Base class for accessing the Netflix API 
 * 
 * @author Richard Hoppes <rhoppes@gmail.com>
 *
 */
class PhpFlix_Netflix 
{	
	protected $_strOAuthKey;
	protected $_strOAuthSecret;
	protected $_strLoginCallback;
	
	public function __construct()
	{
		$this->_strOAuthKey 		= "";
		$this->_strOAuthSecret 		= "";
		$this->_strLoginCallback 	= "";
	}
	
	/**
	 * getOAuthGetUrl
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function getOAuthGetUrl($strUrl, $arrParameters = array(), $strOAuthToken = null, $strOAuthTokenSecret = null) 
	{
		return $this->getOAuthUrl($strUrl, "GET", $arrParameters, $strOAuthToken, $strOAuthTokenSecret);
	} 
	
	
	/**
	 * getOAuthPostUrl
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function getOAuthPostUrl($strUrl, $arrParameters = array(), $strOAuthToken = null, $strOAuthTokenSecret = null) 
	{
		return $this->getOAuthUrl($strUrl, "POST", $arrParameters, $strOAuthToken, $strOAuthTokenSecret);
	} 
	
	
	/**
	 * getOAuthPutUrl
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function getOAuthPutUrl($strUrl, $arrParameters = array(), $strOAuthToken = null, $strOAuthTokenSecret = null) 
	{
		return $this->getOAuthUrl($strUrl, "PUT", $arrParameters, $strOAuthToken, $strOAuthTokenSecret);
	} 
	
	
	/**
	 * getOAuthDeleteUrl
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function getOAuthDeleteUrl($strUrl, $arrParameters = array(), $strOAuthToken = null, $strOAuthTokenSecret = null) 
	{
		return $this->getOAuthUrl($strUrl, "DELETE", $arrParameters, $strOAuthToken, $strOAuthTokenSecret);
	} 
	
	
	/**
	 * parseQueryString
	 * Parses a url and returns an array of query string parameters
	 * 
	 * @param string $strUrl url to parse
	 * @return array parameters
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function parseQueryString($strUrl) 
	{
		$arrParams = array();
	
		$arrParsedUrl = parse_url($strUrl);
		parse_str($arrParsedUrl['query'], $arrParams);
		
		return $arrParams;
	}
	
	
	/**
	 * getOAuthUrl
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	private function getOAuthUrl($strUrl, $strRequestType = "GET", $arrParameters = array(), $strOAuthToken = NULL, $strOAuthTokenSecret = NULL) 
	{
		// Look for additional parameters as part of url
		$arrAdditionalParams = $this->parseQueryString($strUrl);
		
		if($arrAdditionalParams)
		{
			foreach($arrAdditionalParams as $strKey => $mxdValue) 
			{
				$arrParameters[$strKey] = $mxdValue;
			}
		}
		
		// OAuth Consumer
		$objOAuthConsumer = new OAuthConsumer($this->_strOAuthKey, $this->_strOAuthSecret, NULL);
		
		// OAuth Token
		if($strOAuthToken && $strOAuthTokenSecret)
			$objOAuthToken = new OAuthConsumer($strOAuthToken, $strOAuthTokenSecret, NULL);
		else
			$objOAuthToken = NULL;
		
		// OAuthRequest object
		$objOAuthRequest = OAuthRequest::from_consumer_and_token($objOAuthConsumer, $objOAuthToken, $strRequestType, $strUrl, $arrParameters);
		$objOAuthRequest->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $objOAuthConsumer, $objOAuthToken);
		
		// Return the request url
		return $objOAuthRequest->to_url();
	}
	
	/**
	 * postResource
	 * 
	 * Do a post/get, and return the response
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function postResource($strUrl, $strVars = '')
	{
		$hCurl = curl_init();
		curl_setopt($hCurl, CURLOPT_URL, $strUrl);
		curl_setopt($hCurl, CURLOPT_HEADER, false);
		curl_setopt($hCurl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($hCurl, CURLOPT_POST, 1);
		curl_setopt($hCurl, CURLOPT_POSTFIELDS, $strVars);
		
		$strData = curl_exec($hCurl);
		
		curl_close($hCurl);
		
		return $strData;
	}

	/**
	 * getResource
	 * 
	 * Do a post/get, and return the response
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function getResource($strUrl)
	{
		$hCurl = curl_init();
		curl_setopt($hCurl, CURLOPT_URL, $strUrl);
		curl_setopt($hCurl, CURLOPT_HEADER, false);
		curl_setopt($hCurl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);
		
		$strData = curl_exec($hCurl);
		
		curl_close($hCurl);
		
		return $strData;
	}
	
	
	/**
	 * deleteResource
	 * 
	 * Do a post/get, and return the response
	 * 
	 * @author Richard Hoppes <rhoppes@gmail.com>
	 */
	protected function deleteResource($strUrl)
	{
		$hCurl = curl_init();
		curl_setopt($hCurl, CURLOPT_URL, $strUrl);
		curl_setopt($hCurl, CURLOPT_HEADER, false);
		curl_setopt($hCurl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($hCurl, CURLOPT_CUSTOMREQUEST, "DELETE");
		
		$strData = curl_exec($hCurl);
		
		curl_close($hCurl);
		
		return $strData;
	}
}